package com.pig4cloud.pig.environment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.environment.entity.AlarmEntity;
import com.pig4cloud.pig.environment.mapper.AlarmMapper;
import com.pig4cloud.pig.environment.service.AlarmService;
import com.pig4cloud.pig.environment.service.EquipmentService;
import com.pig4cloud.pig.environment.service.SubstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 报警信息
 *
 * @author pig
 * @date 2024-01-15 17:12:41
 */
@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, AlarmEntity> implements AlarmService {
	@Autowired
	private  AlarmMapper alarmMapper;
	@Autowired
	private SubstationService substationService;
	@Autowired
	private EquipmentService equipmentService;


	@Override
	public IPage<AlarmEntity> findPage(Page page, LambdaQueryWrapper<AlarmEntity> lambdaQueryWrapper) {
		IPage<AlarmEntity> list =alarmMapper.selectPage(page,lambdaQueryWrapper);

		Map<Long,String> substationNames=substationService.getNamesByIds(list.getRecords().stream().map(data->{
			return  data.getSubstationId();
		}).collect(Collectors.toList()));

		Map<Long,String> equipmentNames=equipmentService.getNamesByIds(list.getRecords().stream().map(data->{
			return  data.getEquipmentId();
		}).collect(Collectors.toList()));


		list.setRecords(list.getRecords().stream().map(data->{
			data.setSubstationName(substationNames.get(data.getSubstationId()));
			data.setEquipmentName(equipmentNames.get(data.getEquipmentId()));
			return  data;
		}).collect(Collectors.toList()));

		return list;
	}
	@Override
	public AlarmEntity getById(Long id){
		AlarmEntity alarmEntity=alarmMapper.selectById(id);

		List<Long> substationIds =new ArrayList<>();
		substationIds.add(alarmEntity.getSubstationId());
		Map<Long,String> substationNames=substationService.getNamesByIds(substationIds);
		alarmEntity.setSubstationName(substationNames.get(alarmEntity.getSubstationId()));


		List<Long> equipmentIds =new ArrayList<>();
		equipmentIds.add(alarmEntity.getEquipmentId());
		Map<Long,String> equipmentNames=equipmentService.getNamesByIds(equipmentIds);
		alarmEntity.setEquipmentName(equipmentNames.get(alarmEntity.getEquipmentId()));


		return  alarmEntity;
	}


}